package com.example.foo.user;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.example.foo.user.model.User;

public class UserHelper {

	protected static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(UserHelper.class);

	public static Map<String, Object> obtainWechatUser(String appId, User user) {
		Map<String, String> wechatUserMap = JSON.<Map<String, String>>parseObject(user.getWxJson(), Map.class);
		Map<String, Object> wechatUser = JSON.<Map<String, Object>>parseObject(wechatUserMap.get(appId), Map.class);
		return wechatUser;
	}

	public static String obtainWechatUserOpenid(String appId, User user) {
		if (user.getWxJson() == null || user.getWxJson().isEmpty()) {
			log.error("User#{}.wxJson='{}'", user.getId(), user.getWxJson());
		}
		return (String) obtainWechatUser(appId, user).get("openid");
	}

}
